package com.spring.demo.untils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.gson.Gson;
import com.spring.demo.untils.dto.LogDTO;
import com.spring.demo.untils.dto.LogRequest;
import com.spring.demo.untils.dto.OpenRes;
import com.spring.demo.untils.dto.Result;
import com.spring.demo.untils.parse.ResponseParse;
import com.spring.demo.untils.parse.blood.BloodOperateOrderResponseParse;
import com.spring.demo.untils.parse.blood.CreateDoctorOrderResponseParse;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author xuweizhi
 * @since 2023/07/06 10:53
 */
public class OpenUtils {

    public static final String URL = "http://open-zslnyy.cfuture.shop/kapi/hbos-dp/stats/logQuery";
    public static Long SUB_XIN_CHENG = 10012002L;

    public static Long ROUTE_ID = 10019002L;

    public static Gson gson = new Gson();


    public static void main(String[] args) {

        RestTemplate restTemplate = new RestTemplate();

        LogRequest requestDTO = new LogRequest();
        requestDTO.setCurrent(1);
        requestDTO.setPageSize(1000);
        requestDTO.setSubAppId(SUB_XIN_CHENG);
        requestDTO.setRouteId(ROUTE_ID);
        requestDTO.setStart(LocalDateTime.now().minusDays(60L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        requestDTO.setEnd(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        LogDTO openRes2 = handleResult(queryResultStr(restTemplate, requestDTO), new TypeReference<Result<LogDTO>>() {{
        }});

        List<ResponseParse> responseParse = new ArrayList<>();

        responseParse.add(new CreateDoctorOrderResponseParse());
        responseParse.add(new BloodOperateOrderResponseParse());
        openRes2.getDataSource().stream().collect(Collectors.groupingBy(OpenRes::getUri)).forEach((v, openRes) -> {
            responseParse.stream().filter(responseParse1 -> responseParse1.matchParse(v, null))
                    .forEach(parse -> {
                        List<Object> collect = openRes.stream().map(res -> parse.parseRequestBody(res)).filter(Objects::nonNull).collect(Collectors.toList());
                        if(CollectionUtils.isNotEmpty(collect)){
                            parse.execute(collect);
                        }
                    });
        });

        // extracted(openRes2);

    }

    private static void extracted(LogDTO openRes2) {
        List<ResponseParse> responseParse = new ArrayList<>();

        // responseParse.add(new CreateDoctorOrderResponseParse());
        responseParse.add(new CreateDoctorOrderResponseParse());
        responseParse.add(new BloodOperateOrderResponseParse());
        openRes2.getDataSource().stream().collect(Collectors.groupingBy(OpenRes::getUri)).forEach((v, openRes) -> {
            responseParse.stream().filter(responseParse1 -> responseParse1.matchParse(v, null))
                    .forEach(parse -> {
                        List<Object> collect = openRes.stream().map(res -> parse.parseRequestBody(res)).filter(Objects::nonNull).collect(Collectors.toList());
                        if(CollectionUtils.isNotEmpty(collect)){
                            parse.execute(collect);
                        }
                    });
        });
    }

    @Nullable
    private static String queryResultStr(RestTemplate restTemplate, LogRequest requestDTO) {
        HttpEntity<LogRequest> formEntity = getRequestHttpEntity(requestDTO);
        return restTemplate.postForObject(URL, formEntity, String.class);
    }

    @NotNull
    private static <T> HttpEntity<T> getRequestHttpEntity(T data) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Cookie", "dev_hbos_zhongshan_token=aGJvc196aG9uZ3NoYW46N2U1M2E4NWE1ZGQ1NGU4MmIzNWExMGU3NGMyZGM2N2E=.a2VsZTvm/KCjJ4/4GD56+/xwTcswdOqE4Zkkrpiatf+FbaZWmAGzZC94c5DYKyN6OKNaTiCqaDdf3okMAJMjBVqwmAq88l5+6L7TXIfgqS2JuppFeKF6FZvBKrr+FLKb5iSDXO0nk5jayeyn; dev_hbos_his_token=aGJvc19oaXM6YzIzN2VlM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znPsbCwXTHaQzvFnxkfoGpWrD1XROGkEPL9hnJRIsi82nTM/f23ZqOLuImn+gpSdOoHdtDaufpLYawLh0atSMfSqmOmt1ofHw8rjfVqIcfRl8O3Gay0H+FhlDgYQ8Ed1oTt0rwiUI4xSzsk88fG4CBGDz9suH0IdD8OpOC4u9C4dnjU=; prod_hbos_token=YzJmOjJmNWNlNjY1MWM4YTRlZDRhZjMxMzAyNTkxODhhMWE0");
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        return new HttpEntity<>(data, headers);
    }


    private static <T> T handleResult(String resultStr, TypeReference<Result<T>> typeReference) {
        Result<T> result = JSON.parseObject(resultStr, typeReference.getType());
        return result.getData();
    }
}
